package org.shopen.ai.puremvc.controller.project
{
	import flash.events.Event;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.command.SimpleCommand;
	import org.shopen.ai.puremvc.ApplicationConstants;
	import org.shopen.ai.puremvc.model.algo.AlgoProxy;
	import org.shopen.ai.puremvc.model.domain.cities.CountryProxy;
	import org.shopen.ai.puremvc.model.problem.cities.CitiesProblemProxy;
	import org.shopen.ai.puremvc.model.project.ProjectsProxy;
	import org.shopen.ai.puremvc.model.project.vo.Project;
	import org.shopen.ai.puremvc.model.settings.SettingsProxy;
	import org.shopen.ai.puremvc.model.utils.CompleteDelegate;

	public class InitializeProjectCommand extends SimpleCommand
	{		
		override public function execute(notification:INotification):void
		{
			try
			{
				facade.registerProxy(new CountryProxy());
				facade.registerProxy(new AlgoProxy());
				facade.registerProxy(new SettingsProxy());
				facade.registerProxy(new CitiesProblemProxy());
				var proxy : CountryProxy = facade.retrieveProxy(CountryProxy.NAME) as CountryProxy;
				var completeDelegate : CompleteDelegate = new CompleteDelegate();
				completeDelegate.addEventListener(CompleteDelegate.COMPLETE, loadCountryCompleteListener);
				proxy.loadData(completeDelegate);
			}
			catch (e : Error)
			{
				sendNotification(ApplicationConstants.GENERAL_ERROR, e.message);
			}
		}
		
		private function loadCountryCompleteListener(e : Event) : void
		{	
			var proxy : ProjectsProxy = facade.retrieveProxy(ProjectsProxy.NAME) as ProjectsProxy;
			var project : Project = proxy.getCurrentProject();			
			
			sendNotification(ApplicationConstants.PROJECT_INITIALIZED, project);
		}		
	}
}